<?php
class SystemAction extends CommonAction {
	//**********App Setting****************************************
	function app () {
		$appsSelect = $this->getAppsSelectOption();
		$appsSelectHtml = implode(',',$appsSelect);
		$this->assign('appsSelectHtml', $appsSelectHtml );
		$this->display();
	}
	
	function appsjson () {
		$orderinfoHtml['rows'] = $this->getAppsSelect(0);
        echo json_encode($orderinfoHtml);
	}
	
	function appadd() {
		$App = D("Apps");
        if ($vo = $App->create()) {
            $list = $App->add();
            if ($list !== false) {
                $this->success('数据保存成功！');
            } else {
                $this->error('数据写入错误！');
            }
        } else {
            $this->error($App->getError());
        }
	}
	
	 // 更新数据
    public function appedit() {
        $App = D("Apps");
        if ($vo = $App->create()) {
            $list = $App->save();
            if ($list !== false) {
                $this->success('数据更新成功！');
            } else {
                $this->error("没有更新任何数据!");
            }
        } else {
            $this->error($App->getError());
        }
    }

    // 删除数据
    public function appremove() {
		$id = intval( $_POST['form_id'] );
        if (!empty($id)) {
            $App = M("Apps");
			$vo = $App->where("id='{$id}'")->find();
            if ($vo) {
				$App->where("pid='$id'")->delete();
				$result = $App->delete($id);
				if (false !== $result) {
					$this->success('删除成功！');
				} else {
					$this->error('删除出错！');
				}
			}
        } else {
            $this->error('ID错误！');
        }
    }
	
	function getAppsSelect($id = 0,$suojin = ''){
		static $i,$appsSelect;
		$apps = M('Apps')->where("pid='{$id}'")->order('sort')->select();
		foreach( $apps as $row ) {
			if(!isset($i)){
				$i = 0;
			}else{
				$i++;
			}
			$appsSelect[$i]['table_id']['id'] = $row['id'];
			$appsSelect[$i]['table_id']['pid'] = $row['pid'];
			$appsSelect[$i]['table_sort'] = $row['sort'];
			$appsSelect[$i]['table_name'] = $suojin.$row['name'];
			$appsSelect[$i]['table_action'] = $row['action'];
			$appsSelect[$i]['table_info'] = $row['info'];
			$appsSelect[$i]['table_ismenu'] = $row['ismenu'];
			
			$apps_sub = M('Apps')->where("pid='{$row['id']}'")->count();
			
			if( $apps_sub ){
				$this->getAppsSelect($row['id'],$suojin.'──');
			}
		}
		return $appsSelect;
	}
	
	function getAppsSelectOption($id = 0,$suojin = ''){
		static $appsSelect;
		$apps = M('Apps')->where("pid='{$id}'")->order('sort')->select();
		foreach( $apps as $row ) {
			$appsSelect[] = '<option value="'.$row['id'].'">'.$suojin.$row['name'].'</option>';
			$apps_sub = M('Apps')->where("pid='{$row['id']}'")->count();
			
			if( $apps_sub ){
				$this->getAppsSelectOption($row['id'],$suojin.'──');
			}
		}
		return $appsSelect;		
	}
	//*******************App Setting**************************
	
	//*******************Role Setting*************************
	function role () {
		$this->display();
	}
	function rolejson () {
		$roles = M('Role')->select();
		$i = 0;
		foreach( $roles as $row ){
			$roleArr[$i]['table_id'] = $row['id'];
			$roleArr[$i]['table_name'] = $row['name'];
			$roleArr[$i]['table_info'] = $row['info'];
			$roleArr[$i]['table_power'] = $row['power'];
			$roleArr[$i]['table_adppower'] = $row['adppower'];
			$i++;
		}
		$rowsHtml['rows'] = $roleArr;
        echo json_encode($rowsHtml);
	}
	
	function roleadd() {
		$App = D("Role");
        if ($vo = $App->create()) {
            $list = $App->add();
            if ($list !== false) {
                $this->success('数据保存成功！');
            } else {
                $this->error('数据写入错误！');
            }
        } else {
            $this->error($App->getError());
        }
	}
	
	 // 更新数据
    public function roleedit() {
        $App = D("Role");
        if ($vo = $App->create()) {
            $list = $App->save();
            if ($list !== false) {
                $this->success('数据更新成功！');
            } else {
                $this->error("没有更新任何数据!");
            }
        } else {
            $this->error($App->getError());
        }
    }

    // 删除角色
	//@todo 是否同时删除角色下用户
    public function roleremove() {
		$id = intval( $_POST['form_id'] );
        if (!empty($id)) {
            $App = M("Role");
			$vo = $App->where("id='{$id}'")->find();
            if ($vo) {
				$result = $App->delete($id);
				if (false !== $result) {
					$this->success('删除成功！');
				} else {
					$this->error('删除出错！');
				}
			}
        } else {
            $this->error('ID错误！');
        }
    }
	
	
	function rolepower( ) {
		$roleID = intval($_GET['roleid']);
		$ida = array();
		if( $roleID ) {
			$powerArr = D('Role')->where("id='{$roleID}'")->find();
			if( $powerArr['power'] ){
			$ida = @explode(',',$powerArr['power']);
			$ida = array_unique($ida);
			}
			$state = 'open';
		}
		$power = $this->getPowerSelect($state,$ida, 0);
		echo json_encode($power);
	}
	
	/**
	 * 权限
	 * @param $state closed | open
	 * @param $ida 	1,2,3,4
     * @param @id   roleID
	 **/
	function getPowerSelect($state, $ida, $id = 0){
		$apps = D('Apps')->where("pid='{$id}'")->order('sort')->select();
		$i = 0;
		foreach ($apps as $row ){
			$power[$i]['id'] = $row['id'];
			$power[$i]['text'] = $row['name'];
			if(in_array($row['id'],$ida)){
				$power[$i]['checked'] = true;
			}
			$apps_sub = D('Apps')->where("pid='{$row['id']}'")->count();
			if( $apps_sub ){
				$power[$i]['state'] = $state;
				$power[$i]['children'] = $this->getPowerSelect($state,$ida,$row['id']);
			}
			$i++;
		}
		return $power;
	}
	//***************Role Setting**************************

	//***************User Setting**************************
	function user () {
		
		$roles = M('Role')->select();
		foreach( $roles as $row ){
			$roleSelect[] = '<option value="'.$row['id'].'">'.$row['name'].'</option>';
		}
		$roleSelectHtml = implode(',',$roleSelect);
		$this->assign('roleSelectHtml', $roleSelectHtml );
		$this->display();
	}
	
	function userjson () {
		$page = max(1, intval( $_POST['page'] ) );
		$rows = intval( $_POST['rows'] ) ? intval( $_POST['rows'] ) : 20;
		$limitstart = $rows*($page-1);
        $list = M('User')->limit( $limitstart , $rows)->order('ID DESC')->select();
		$row_tot = M('User')->count();
		$i=0;
		foreach( $list as $row ){
			$roleRet = M('Role')->where("id='{$row['role']}'")->field('name')->find();
			$userArr[$i]['table_id'] = $row['id'];
			$userArr[$i]['table_account'] = $row['account'];
			$userArr[$i]['table_name'] = $row['nickname'];
			$userArr[$i]['table_email'] = $row['email'];
			$userArr[$i]['table_role']['role'] = $row['role'];
			$userArr[$i]['table_role']['rolename'] = $roleRet['name'];
			$i++;
		}
		$userHtml['total'] = $row_tot;
		$userHtml['rows'] = $userArr;
        echo json_encode($userHtml);
	}
	
	function useradd() {
		$App = D("User");
        if ($vo = $App->create()) {
            $list = $App->add();
            if ($list !== false) {
                $this->success('数据保存成功！');
            } else {
                $this->error('数据写入错误！');
            }
        } else {
            $this->error($App->getError());
        }
	}
	
	 // 更新数据
    public function useredit() {
        $App = D("User");
        if ($vo = $App->create()) {
            $list = $App->save();
            if ($list !== false) {
                $this->success('数据更新成功！');
            } else {
                $this->error("没有更新任何数据!");
            }
        } else {
            $this->error($App->getError());
        }
    }

    // 删除数据
    public function userremove() {
		$id = intval( $_POST['form_id'] );
        if (!empty($id)) {
            $App = M("User");
			$vo = $App->where("id='{$id}'")->find();
            if ($vo) {
				$App->where("pid='$id'")->delete();
				$result = $App->delete($id);
				if (false !== $result) {
					$this->success('删除成功！');
				} else {
					$this->error('删除出错！');
				}
			}
        } else {
            $this->error('ID错误！');
        }
    }
	
	 
	
	
	//***************User Setting**************************



}